'''
Case Type： 故障&可靠性测试
Case Name： 备节点故障	临时表操作过程中dn备故障
Create At: 2020/07/02
@zou_jialiang0505328114
Descption:
步骤 1.启动数据库
步骤 2.开始执行dml操作
步骤 3.kill备进程
步骤 4.查看数据库状态
步骤 5.重新拉起数据库
步骤 6.查看数据库状态
'''
import unittest
import _thread
import time
from yat.test import Node
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import *


class Deletaduit(unittest.TestCase):
    sh_primy = CommonSH('PrimaryDbUser')
    sh_standby = CommonSH('Standby1DbUser')
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info('------------Opengauss_Reliability_Dbsys_Case005.py start------------')
        self.rootNode = Node()
        self.primary_dbuser = Node(node='PrimaryDbUser')
        self.primary_root = Node(node='PrimaryRoot')
        self.standby1_root = Node(node='Standby1Root')
        self.standby1_dbuser = Node(node='Standby1DbUser')
        self.com = Common()
        self.Constant = Constant()
        self.t_name = 't_test'

    def test_startdb(self):
        # 建表
        self.log.info('----------------------------创建表，并写入数据-----------------------------')
        # msg = self.com.createLargeNumTable(self.primary_dbuser, self.t_name, 1, 1)
        # self.log.info(msg)
        sql = f'''
                drop table if exists {self.t_name};
                create table {self.t_name}(t_id integer, t_desc character(20));
                '''
        self.log.info(sql)
        msg = self.sh_primy.executDbSql(sql)
        self.log.info(msg)

        insert_sql = f"insert into {self.t_name} values (1,'aaa');"
        self.log.info('开始执行dml操作')
        _thread.start_new_thread(self.sh_primy.cycle_exec_sql, (insert_sql, 1000000,))

        time.sleep(1)

        self.log.info('kill备进程')
        msg = self.com.kill_pid(self.standby1_root, 9)
        self.assertEqual(msg, '')

        self.log.info('查看数据库状态')
        status = self.sh_primy.getDbClusterStatus("status")
        self.log.info(status)
        self.assertFalse(status)

        # self.log.info('重新拉起数据库')
        # self.sh_standby.startDbCluster()

        self.log.info('备库启动')
        msg = self.sh_standby.executeGsctl('start', self.Constant.REBUILD_SUCCESS_MSG, '-M standby')
        self.log.info(msg)
        self.assertTrue(msg)

        self.log.info('查看数据库状态')
        status = self.sh_standby.getDbClusterStatus("status")
        self.log.info(status)
        self.assertTrue(status)

        time.sleep(15)

    def tearDown(self):
        # 清理环境，删除表
        # msg = self.com.createLargeNumTable(self.primary_dbuser, self.t_name, del_sign=True)
        # self.log.info(msg)
        sql = f'''drop table if exists {self.t_name};'''
        self.log.info(sql)
        msg = self.sh_primy.executDbSql(sql)
        self.log.info(msg)

        db_status = self.sh_primy.getDbClusterStatus('status')
        if not db_status:
            is_started = self.sh_primy.startDbCluster()

            self.assertTrue(is_started)
        else:
            pass
